import ChatSessionPageStateStore from "@/store/chat-session-page";

export const useChatSessionPageState = () =>  ChatSessionPageStateStore((state) => state.state);

export const useChatSessionPageActions = () => {
    const actions = ChatSessionPageStateStore((state) => state.actions);
    const {selectedSessions} = ChatSessionPageStateStore((state) => state.state)

    function toggleSessionSelections(sessionIds: string[]) {
        if (sessionIds.length === 0)
            return;
        sessionIds.forEach((sessionId) => {
            if (!selectedSessions.includes(sessionId)) {
                actions.addSelectedSession(sessionId);
            } else {
                actions.removeSelectedSession(sessionId);
            }
        });
    }

    function selectBatchSessions(sessions: string[]){
        sessions.forEach((sessionId) => {
            if (!selectedSessions.includes(sessionId)) {
                actions.addSelectedSession(sessionId);
            }
        });
    }

    function unSelectBatchSessions(sessions: string[]) {
        sessions.forEach((sessionId) => {
            if (selectedSessions.includes(sessionId)) {
                actions.removeSelectedSession(sessionId);
            }
        });
    }

    function toggleSessionSelection(sessionId: string) {
        if (!selectedSessions.includes(sessionId)) {
            actions.addSelectedSession(sessionId);
        } else {
            actions.removeSelectedSession(sessionId);
        }
    }

    return {
        ...actions,
        selectBatchSessions,
        unSelectBatchSessions,
        toggleSessionSelection,
        toggleSessionSelections
    };
};